import java.util.Stack;

class Solution1 {
    public boolean backspaceCompare(String s, String t) {
        String str1 = change(s);
        String str2 = change(t);
        return str1.equals(str2);
    }
    public String change(String str) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!stack.empty() && c == '#') {
                stack.pop();
        } else if (c != '#'){
                stack.push(c);
            }
        }
        StringBuilder ret = new StringBuilder();
        for (char c: stack) {
            ret.append(c);
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        Solution1 s = new Solution1();
        System.out.println(s.backspaceCompare("y#fo##f", "y#f#o##f"));
    }
}